package com.google.code.mamule.process;

import com.google.code.mamule.mail.Mail;
import com.google.code.mamule.mail.Mailable;
import com.google.code.mamule.print.PrintDocument;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;

/**
 * Created with IntelliJ IDEA. User: borck_000 ; Date: 12/25/13 ; Time: 5:27 PM
 */
public class MockRenderer implements MailRenderer {

  private final static Logger log = LoggerFactory.getLogger(MockRenderer.class);

  @Nullable
  @Override
  public Iterable<PrintDocument> apply(@Nullable final Mail input) {
    log.info("rendering {}", input);
    Collection<Mailable> receivers = input.getReceiver();
    if (receivers.isEmpty()) {
      return Collections.singleton(render(input.getSender(), null, input.getText()));
    }
    List<PrintDocument> docs = new ArrayList<>(receivers.size());
    for (Mailable each : receivers) {
      docs.add(render(input.getSender(), each, input.getText()));
    }
    return docs;
  }

  private PrintDocument render(Mailable sender, Mailable receiver, String text) {
    return new PrintDocument(
        Objects.toString(sender, "no sender ):"),
        Objects.toString(receiver, "no receiver ):"),
        text);
  }
}
